
//THIS CLASS INCLUDES STATIC METHOD TO CONVERT A TEST INTO A PLAIN TEXT FILE AND CALL A EXTERNAL METHOD (FILEMANAGER) TO SAVE IT

//THIS CLASS INCLUDES STATIC METHOD WHICH RECEIVES A BUFFERED READER (FILEMANAGER) AND CONVERTS IT INTO A TEST OBJECT

package model.test;

import java.util.ArrayList;
import model.fileManager.file;


/**
 *
 * @author FELIPE
 */
public class OpsFile {
    
    //this method takes the especified Test object and converts it into a string usin toString() and saves it into a .txt file    
    public static void writeTestFile(Test test,String fileName){
        
        String pathName= System.getProperty("user.dir")+"/src/savedTestFiles/"+fileName+".txt";// Route and name (with extension) of the file we want to WRITE
        file.saveFile(pathName,test.toString());
        
    }
    
    
    
    //this method reads a file, converts it into a Test with all its elements inside and returns it 
    //THIS METHOD USES THE ESPECIAL CONSTRUCTOR, WHICH CREATES ITSELF AND THEN CREATES ITS KIDS
    //Uses constructor type 2
    public static Test readTestFile(String fileName){
       
       String readResult=file.openFile(fileName);
       Test resultingTest = new Test(readResult);
       return resultingTest; //returns the created test with all the information inside the file
       
       
    }//readTestFile
    
    
    
    //this method reads a file, converts it into a Test with all its elements inside and returns it 
    // IT CONSTRUCTS EACH ELEMENT MANUALLY AND ADDS IT TO THE PARENT ELEMENT
    //uses constructor type 1
    
    //THIS IS MISSING ADDING THE PENALTY
    
    /*
     * 
     * 
    public static Test readTestFile2(String fileName){
       
       
       //String fileName= System.getProperty("user.dir")+"/src/savedTestFiles/EnglishTest.txt";// Route and name (with extension) of the file we want to READ
       String readResult=file.openFile(fileName);
       
       
       
       //convert the resulting string read from the file into a test object with all its elements (sections, subsections, questions)
       
       String[] readResultTest=readResult.split("\r"); //readResultTest is an array that contain the file read, which each element in the array is each line
                                                       //readResultTest represents a Test, first elements are info and after that each element is a section
       
       String nameTest = readResultTest[1];     //data about the test
       String nameAuthor = readResultTest[2];
       String descTxtTest=readResultTest[3];
       
       Test resultingTest = new Test(nameTest,nameAuthor,descTxtTest);//create the Test object with the information given
       
       //create Sections and obtain subsections within it
       for(int i=4;i<readResultTest.length;i++){
           
           String[] readResultSection = readResultTest[i].split("@"); //readResultSection is an array that represents each Section, which contains info and in each element a subsection
           String nameSection = readResultSection[0];       //Data about Section
           String timeLimitSection = readResultSection[1];
           String descTxtSection = readResultSection[2];
           Section resultingSection = new Section(nameSection,Integer.parseInt(timeLimitSection),descTxtSection); //create a Section object //take into account that in each loop of the while, Section variable will contain a different section
           Ops.ADDsectionTOtest(resultingSection, resultingTest); //add section created to the test
          
           
           //create Subsections and obtain questions within it
           for (int j=3;j<readResultSection.length;j++){
               
               String[] readResultSubSection = readResultSection[j].split("%");//readResultSubSetion is an array that represents each subsection, which contain info and in each element a question
               String nameSubSection = readResultSubSection[0];     //data about the subsection
               String dscTxtSubSection = readResultSubSection[1];
               SubSection resultingSubsection = new SubSection(nameSubSection,dscTxtSubSection); //create a Subsection object //
               Ops.ADDsubSectionTOsection(resultingSubsection, resultingSection);//add subsection to the section
               
               //create question and obtain info within it
               for (int k=2;k<readResultSubSection.length;k++){
                   
                   String[] readResultQuestion = readResultSubSection[k].split("~"); //readResultQuestion is an array that represents each question, which contain the info about the question
                   
                   String typeQuestion = readResultQuestion[0]; //common data to all questions
                   String textQuestion = readResultQuestion[1];
                   int marksQuestion =Integer.parseInt(readResultQuestion[2]);
                   
                   Question resultingQuestion; //create question but do not initialize it until we know the type of question
                   
                   
                   //get additional info of especific type of question and initialize questions depending on the type
                   if (typeQuestion.equals("MCQ")){
                       
                       
                       int corrAnsQuestion =Integer.parseInt(readResultQuestion[3]); //correct answer from the options (index)
                       String[] readResultPossAns = readResultQuestion[4].split("&"); //split into possible answers
                       ArrayList<String> possAns = new ArrayList<String>();           //create ArrayList to construct the possible answers and send to constructor of MCQ
                       
                       for (int l=0;l<readResultPossAns.length;l++){
                           possAns.add(readResultPossAns[l]);
                       }
                       
                       resultingQuestion=new MCQ(textQuestion, marksQuestion,possAns,corrAnsQuestion);              //initialize question
                       Ops.ADDquestionTOsubSection(resultingQuestion, resultingSubsection); //add question to the subsection
                       
                   }
                   else if (typeQuestion.equals("FIBQ")){
                       
                       String[] readResultCorrAns = readResultQuestion[3].split("&"); //split into correct answers
                       
                       ArrayList<String> corrAns = new ArrayList<String>();           //create ArrayList to construct the correct answers and send to constructor of FIBQ
                       
                       for (int l=0;l<readResultCorrAns.length;l++){
                           corrAns.add(readResultCorrAns[l]);
                       }
                       
                       resultingQuestion=new FIBQ(textQuestion, marksQuestion,corrAns);             //initialize question
                       Ops.ADDquestionTOsubSection(resultingQuestion, resultingSubsection); //add question to the subsection
                   }
                   
                   
                   
               }
               
               
           }//for j
           
           
       }//for i
       
       
       return resultingTest; //returns the created test with all the information inside the file
       
       
    }//readTestFile2
    * 
    */
    

    //FOR TESTING PURPOSE
    public static void main(String[] args) {
		 
        //readTestFile();

    }
    
}
